package com.jiamao.goplay.ui.welcome;

import android.app.Application;

import androidx.annotation.NonNull;

import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.main.MainActivity;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.tamsiree.rxkit.view.RxToast;

import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.SPUtils;

public class WelComeViewModel extends BaseViewModel<DataRepository> {
    public WelComeViewModel(@NonNull Application application) {
        super(application);
    }

    public WelComeViewModel(@NonNull Application application, DataRepository model) {
        super(application, model);



    }

    public void toMain(){


    }



        public void initIMSDk(){
           if(SPUtils.getInstance().getBoolean(Config.IS_LOGIN)){
               NimUIKit.login(new LoginInfo(model.getmLocalDataSource().getUid()+"", model.getmLocalDataSource().getIMToken()), new RequestCallback<LoginInfo>() {
                   @Override
                   public void onSuccess(LoginInfo param) {

                       SPUtils.getInstance().put(Config.IS_LOGIN,true);

                       AppManager.getAppManager().finishAllActivity();
                       startActivity(MainActivity.class);
                   }

                   @Override
                   public void onFailed(int code) {
                       RxToast.showToastShort("IM 登录失败"+code);
//                                        if (code == 302 || code == 404) {
//                                            RxToast.showToastShort("IM 登录失败");
//                                            Toast.makeText(LoginActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
//                                        } else {
//                                            Toast.makeText(LoginActivity.this, "登录失败: " + code, Toast.LENGTH_SHORT).show();
//                                        }
                   }

                   @Override
                   public void onException(Throwable exception) {
                       RxToast.showToastShort("IM 登录异常"+exception.getMessage());
                   }
               });
            }


        }

    public void addRxSubscribe(Disposable disposable){
        addSubscribe(disposable);
    }

}
